<?php

include_once 'Classes/Trip.class.php';
class TripDAO {

    // Syndesh kai epilogh bashs
    function __construct(){
        $this->connection = mysql_connect('127.0.0.1', 'root', '')
                or die('Could not connect: ' . mysql_error());
        mysql_select_db('travelbook', $this->connection)
                or die('Could not select database');
    }

    // Kleisimo syndeshs me thn bash
    function __destruct(){
        if(isset($connection))
            mysql_close($connection);
    }

    function insertTrip($trip){
        $query = "INSERT INTO trip VALUES('$trip->tripId','$trip->userid','$trip->fromcityid','$trip->tocityid','$trip->fromdate','$trip->todate','$trip->transport','$trip->description')";
        mysql_query($query);
    }

    function getRecentTrips(){
        $maxidquery = 'select max(tripid) from trip';
        $maxidResult =  mysql_query($maxidquery);
        $row = mysql_fetch_array($maxidResult);
        $maxid = $row['max(tripid)'];
        //$query = "SELECT * FROM trip WHERE tripid= '$maxid' ";
        $query = "select * from trip order by tripid limit 4";
        $result = mysql_query($query);

        $trips = array();
        while($row = mysql_fetch_array($result)){
            $trips[] = new Trip($row['tripid'], $row['userid'], $row['fromcityid'], $row['tocityid'], $row['fromdate'], $row['todate'], $row['transport'], $row['description']);
        }
       return $trips;
    }

    // Epistrefei ta 5 pio prosfata taksidia twn xrthstwn poy kanei follow
    // o syndedemenos xrhsths.
    function getFollowingsRecentTrips($userid){
        $query = "SELECT * FROM trip WHERE userid=(SELECT followingid FROM follow WHERE followerid='$userid') ORDER BY fromdate DESC LIMIT 4";
        $result = mysql_query($query);
        $tripArr = array();
        while ($row = mysql_fetch_array($result)){
            $trip = new Trip($row['tripid'],
                                $row['userid'],
                                $row['fromcityid'],
                                $row['tocityid'],
                                $row['fromdate'],
                                $row['todate'],
                                $row['transport'],
                                $row['description']);
            $tripArr[] = $trip;
        }
        return $tripArr;
    }

    // Epistrefei ta taksidia poy ekane o xrhsths kata to parelthon
    function retrievePastTrips($userid){
        $query = "SELECT * FROM trip WHERE userid=$userid AND todate<curdate()";
        $result = mysql_query($query);
        $tripArr = array();
        while ($row = mysql_fetch_array($result)){
            $trip = new Trip($row['tripid'],
                                $row['userid'],
                                $row['fromcityid'],
                                $row['tocityid'],
                                $row['fromdate'],
                                $row['todate'],
                                $row['transport'],
                                $row['description']);
            $tripArr[] = $trip;
        }
        return $tripArr;
    }

    // Epistrefei ta taksidia poy tha kanei o xrhsths sto mellon
    function retrieveFutureTrips($userid){
        $query = "SELECT * FROM trip WHERE userid=$userid AND fromdate>curdate()";
        $result = mysql_query($query);
        $tripArr = array();
        while ($row = mysql_fetch_array($result)){
            $trip = new Trip($row['tripid'],
                                $row['userid'],
                                $row['fromcityid'],
                                $row['tocityid'],
                                $row['fromdate'],
                                $row['todate'],
                                $row['transport'],
                                $row['description']);
            $tripArr[] = $trip;
        }
        return $tripArr;
    }

}
?>
